<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta name="google-site-verification" content="-YPbgjltFVFljoCm04Ktk0m38A8CkQxpifuKVZXtawA">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">






















<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/avatar.gif?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/avatar.gif?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/avatar.gif?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/avatar.gif?v=5.1.4" color="#222">





  <meta name="keywords" content="vue,vue-cli,">





  <link rel="alternate" href="/blog/atom.xml" title="lfagon" type="application/atom+xml">






<meta name="description" content="介绍 vue-router是一个组件跳转控制，是一个vue的插件，叫做vue-router。在官网有下详细介绍。它将一个组件绑定一个路径地址，当我们访问该路径地址的时候，就会跳到相应的组件页面中。一个组件页面被渲染在&amp;lt;router-view&amp;gt;&amp;lt;/router-view&amp;gt;中，多级路由也是如此。例如：当时二级路由时，其匹配的&amp;lt;router-view&amp;gt;&amp;lt;/rou">
<meta name="keywords" content="vue,vue-cli">
<meta property="og:type" content="article">
<meta property="og:title" content="vue-router">
<meta property="og:url" content="http://lfagon.gitee.io/blog/2019/06/12/vue-router/index.html">
<meta property="og:site_name" content="lfagon">
<meta property="og:description" content="介绍 vue-router是一个组件跳转控制，是一个vue的插件，叫做vue-router。在官网有下详细介绍。它将一个组件绑定一个路径地址，当我们访问该路径地址的时候，就会跳到相应的组件页面中。一个组件页面被渲染在&amp;lt;router-view&amp;gt;&amp;lt;/router-view&amp;gt;中，多级路由也是如此。例如：当时二级路由时，其匹配的&amp;lt;router-view&amp;gt;&amp;lt;/rou">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-06-21T06:56:01.195Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="vue-router">
<meta name="twitter:description" content="介绍 vue-router是一个组件跳转控制，是一个vue的插件，叫做vue-router。在官网有下详细介绍。它将一个组件绑定一个路径地址，当我们访问该路径地址的时候，就会跳到相应的组件页面中。一个组件页面被渲染在&amp;lt;router-view&amp;gt;&amp;lt;/router-view&amp;gt;中，多级路由也是如此。例如：当时二级路由时，其匹配的&amp;lt;router-view&amp;gt;&amp;lt;/rou">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":true,"onmobile":false},
    fancybox: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://lfagon.gitee.io/blog/2019/06/12/vue-router/">





  <title>vue-router | lfagon</title>
  








  <!-- Global site tag (gtag.js) - Google Analytics -->
  <script async src="https://www.googletagmanager.com/gtag/js?id=UA-139884627-1"></script>
  <script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());
    gtag('config', 'UA-139884627-1');
  </script>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">

    <div class="headband"></div>
    

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/" class="brand" rel="start">
        <!-- <span class="logo-line-before"><i></i></span> -->
        <!-- <span class="site-title">lfagon</span> -->
	     <img src="/blog/images/logo.png">
        <!-- <span class="logo-line-after"><i></i></span> -->
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">个人博客</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-top">
          <a href="/blog/top/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-signal"></i> <br>
            
            热门
          </a>
        </li>
      
        
        <li class="menu-item menu-item-schedule">
          <a href="/blog/schedule/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-calendar"></i> <br>
            
            日程表
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tracks">
          <a href="/blog/tracks" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-comment"></i> <br>
            
            留言
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://lfagon.gitee.io/blog/blog/2019/06/12/vue-router/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="付祥/lfagon">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/blog/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="lfagon">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">vue-router</h2>
        

        <div class="post-meta">
	    
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <font color="blue"><i class="fa fa-calendar-o"></i></font>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-06-12T08:58:02+08:00">
                2019-06-12
              </time>
            

            
              <span class="post-meta-divider">|</span>
            

            
              <span class="post-meta-item-icon">
                 <font color="#40E0D0"><i class="fa fa-calendar-check-o"></i></font>
              </span>
              
                <span class="post-meta-item-text">更新于&#58;</span>
              
              <time title="更新于" itemprop="dateModified" datetime="2019-06-21T14:56:01+08:00">
                2019-06-21
              </time>
            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <font color="red"><i class="fa fa-folder-o"></i></font>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <font color="black"><span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/用法/" itemprop="url" rel="index">
                    <span itemprop="name">用法</span>
                  </a>
                </span></font>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/blog/2019/06/12/vue-router/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count gitment-comments-count" data-xid="/blog/2019/06/12/vue-router/" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  2.2k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  9
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1>介绍</h1>
<p>vue-router是一个组件跳转控制，是一个vue的插件，叫做vue-router。在<a href="https://router.vuejs.org/zh/" target="_blank" rel="noopener">官网</a>有下详细介绍。它将一个组件绑定一个路径地址，当我们访问该路径地址的时候，就会跳到相应的组件页面中。一个组件页面被渲染在<code>&lt;router-view&gt;&lt;/router-view&gt;</code>中，多级路由也是如此。例如：当时二级路由时，其匹配的<code>&lt;router-view&gt;&lt;/router-view&gt;</code>也必定有两个。</p>
<h1>安装与配置</h1>
<ol>
<li>
<p>因此当在 vue 工程时，我们需要同 npm 安装 vue-router。</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install vue-router --save</span><br></pre></td></tr></table></figure>
</li>
<li>
<p>导入路由</p>
<p>在 vue 工程里，源代码目录 src 下会有 main.js 文件，它是 vue 工程的路口。因此我们安装好插件之后，我们必须在 main.js 文件里注册。</p>
<p><strong>main.js</strong></p>
 <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">'vue'</span>;<span class="comment">//vue工程里，main.js里一定会有这个</span></span><br><span class="line"><span class="keyword">import</span> VueRouter <span class="keyword">from</span> <span class="string">'vue-router'</span>;<span class="comment">//导入路由</span></span><br><span class="line">Vue.use(VueRouter);<span class="comment">//注册导入进来的路由组件</span></span><br></pre></td></tr></table></figure>
</li>
<li>
<p>配置路由</p>
 <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* jshint esversion: 6 */</span></span><br><span class="line">impot 组件名 <span class="keyword">from</span> <span class="string">'组件位置'</span>;</span><br><span class="line">impot 组件名<span class="number">1</span> <span class="keyword">from</span> <span class="string">'组件位置'</span>;</span><br><span class="line">impot 组件名<span class="number">2</span> <span class="keyword">from</span> <span class="string">'组件位置'</span>;</span><br><span class="line"><span class="keyword">const</span> RouterList=[<span class="comment">//配置路由列表</span></span><br><span class="line">    path:<span class="string">''</span>,<span class="comment">//路由路径</span></span><br><span class="line">    name:<span class="string">''</span>,<span class="comment">//命名路由</span></span><br><span class="line">    component: <span class="string">'组件名'</span>,<span class="comment">//引用导入进来的组件</span></span><br><span class="line">    children:[<span class="comment">//是否配置子集路由，子集路由也是一样，参照顶层配置，可以配置多级路由</span></span><br><span class="line">        &#123;</span><br><span class="line">            path:<span class="string">''</span>,</span><br><span class="line">            name:<span class="string">''</span>,<span class="comment">//命名路由</span></span><br><span class="line">            component: <span class="string">'组件名1'</span>,</span><br><span class="line">            children:[</span><br><span class="line">                ......</span><br><span class="line">            ]</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            path:<span class="string">''</span>,</span><br><span class="line">            name:<span class="string">''</span>,<span class="comment">//命名路由</span></span><br><span class="line">            component: <span class="string">'组件名2'</span>,</span><br><span class="line">            children:[</span><br><span class="line">                ......</span><br><span class="line">            ]</span><br><span class="line">        &#125;</span><br><span class="line">        ......</span><br><span class="line">    ]</span><br><span class="line">];</span><br><span class="line"><span class="keyword">const</span> RouterConfig=&#123;<span class="comment">//配置路由参数</span></span><br><span class="line">    mode:<span class="string">'histroy'</span>,<span class="comment">//使用html5历史模式，如果不配置就会使用#锚点来进行跳转</span></span><br><span class="line">    router:RouterList<span class="comment">//配置路由映射列表</span></span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">const</span> router=<span class="keyword">new</span> VueRouter(RouterConfig);<span class="comment">//RouterConfig路由配置生成路由对象</span></span><br></pre></td></tr></table></figure>
</li>
<li>
<p>注册路由</p>
 <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">new</span> Vue(&#123;</span><br><span class="line">    ......</span><br><span class="line">    render: <span class="function"><span class="params">h</span> =&gt;</span> h(App),</span><br><span class="line">    router: router<span class="comment">//在vue中注册路由对象注册路由对象</span></span><br><span class="line">    ......</span><br><span class="line">&#125;).$mount(<span class="string">'#app'</span>);</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h1>优化路由配置</h1>
<p>如果说页面很多，工程比较大的话，那么 main.js 文件会有很多不同组件的配置，而且页面路由配置一定会很长。这样不容易管理。因此这里我将路由路径映射归到一个文件中，然后通过 import 语句导入到 main.js 中，这样会比较清晰。</p>
<p>新建<strong>VueRouterConfig.js</strong>，与<strong>main.js</strong>同级</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* jshint esversion: 6 */</span></span><br><span class="line">impot 组件名 <span class="keyword">from</span> <span class="string">'组件位置'</span>;</span><br><span class="line">impot 组件名<span class="number">1</span> <span class="keyword">from</span> <span class="string">'组件位置'</span>;</span><br><span class="line">impot 组件名<span class="number">2</span> <span class="keyword">from</span> <span class="string">'组件位置'</span>;</span><br><span class="line"><span class="keyword">const</span> RouterList=[<span class="comment">//配置路由列表</span></span><br><span class="line">    path:<span class="string">''</span>,<span class="comment">//路由路径</span></span><br><span class="line">    name:<span class="string">''</span>,<span class="comment">//命名路由</span></span><br><span class="line">    component: <span class="string">'组件名'</span>,<span class="comment">//引用导入进来的组件</span></span><br><span class="line">    children:[<span class="comment">//是否配置子集路由，子集路由也是一样，参照顶层配置，可以配置多级路由</span></span><br><span class="line">        &#123;</span><br><span class="line">            path:<span class="string">''</span>,</span><br><span class="line">            name:<span class="string">''</span>,<span class="comment">//命名路由</span></span><br><span class="line">            component: <span class="string">'组件名1'</span>,</span><br><span class="line">            children:[</span><br><span class="line">                ......</span><br><span class="line">            ]</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            path:<span class="string">''</span>,</span><br><span class="line">            name:<span class="string">''</span>,<span class="comment">//命名路由</span></span><br><span class="line">            component: <span class="string">'组件名2'</span>,</span><br><span class="line">            children:[</span><br><span class="line">                ......</span><br><span class="line">            ]</span><br><span class="line">        &#125;</span><br><span class="line">        ......</span><br><span class="line">    ]</span><br><span class="line">];</span><br><span class="line"><span class="keyword">const</span> VueRouterConfig=&#123;<span class="comment">//配置路由参数</span></span><br><span class="line">    mode:<span class="string">'histroy'</span>,<span class="comment">//使用html5历史模式，如果不配置就会使用#锚点来进行跳转</span></span><br><span class="line">    router:RouterList<span class="comment">//配置路由映射列表</span></span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> VueRouterConfig;<span class="comment">//导出路由配置</span></span><br></pre></td></tr></table></figure>
<p><strong>main.js 文件</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> VueRouter <span class="keyword">from</span> <span class="string">'vue-router'</span>;</span><br><span class="line">Vue.use(VueRouter);</span><br><span class="line"><span class="keyword">import</span> VueRouterConfig <span class="keyword">from</span> <span class="string">'./VueRouterConfig'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> router = <span class="keyword">new</span> VueRouter(RouterConfig);</span><br><span class="line"><span class="keyword">new</span> Vue(&#123;</span><br><span class="line">  render: <span class="function"><span class="params">h</span> =&gt;</span> h(App),</span><br><span class="line">  router: router</span><br><span class="line">&#125;).$mount(<span class="string">'#app'</span>);</span><br></pre></td></tr></table></figure>
<h1>匹配优先级</h1>
<p>有时候，同一个路径可以匹配多个路由，此时，匹配的优先级就按照路由的定义顺序：谁先定义的，谁的优先级就最高。</p>
<h1>捕获所有路由或 404 路由</h1>
<p>常规参数只会匹配被 / 分隔的 URL 片段中的字符。如果想匹配任意路径，我们可以使用通配符<code>*</code>：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">······</span><br><span class="line">&#123;</span><br><span class="line">  <span class="comment">// 会匹配所有路径</span></span><br><span class="line">  path: <span class="string">'*'</span>,</span><br><span class="line">   component: 组件名</span><br><span class="line">&#125;</span><br><span class="line">&#123;</span><br><span class="line">  <span class="comment">// 会匹配以/user-开头的任意路径</span></span><br><span class="line">  path: <span class="string">'/user-*'</span>,</span><br><span class="line">   component: 组件名</span><br><span class="line">&#125;</span><br><span class="line">······</span><br></pre></td></tr></table></figure>
<p>当使用通配符路由时，请确保路由的顺序是正确的，也就是说含有通配符的路由应该放在最后。路由<code>{ path: '*' }</code>通常用于客户端 404 错误。如果你使用了 History 模式，请确保正确配置你的服务器。</p>
<p>当使用一个通配符时，<code>$route.params</code>内会自动添加一个名为<code>pathMatch</code>参数。它包含了 URL 通过通配符被匹配的部分：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 给出一个路由 &#123; path: '/user-*' &#125;</span></span><br><span class="line"><span class="keyword">this</span>.$router.push(<span class="string">'/user-admin'</span>)</span><br><span class="line"><span class="keyword">this</span>.$route.params.pathMatch <span class="comment">// 'admin'</span></span><br><span class="line"><span class="comment">// 给出一个路由 &#123; path: '*' &#125;</span></span><br><span class="line"><span class="keyword">this</span>.$router.push(<span class="string">'/non-existing'</span>)</span><br><span class="line"><span class="keyword">this</span>.$route.params.pathMatch <span class="comment">// '/non-existing'</span></span><br></pre></td></tr></table></figure>
<h1>路由跳转</h1>
<h2>声明式跳转</h2>
<p><strong><code>&lt;router-link :to=&quot;location&quot;&gt;</code></strong></p>
<h2>编程式跳转</h2>
<p><strong>注意：在 Vue 实例内部，你可以通过<code>$router</code>访问路由实例。</strong></p>
<ol>
<li><strong><code>router.push(location, onComplete?, onAbort?)</code></strong></li>
</ol>
<p><code>onComplete</code>和<code>onAbort</code>回调作为第二个和第三个参数。这些回调将会在导航成功完成 （在所有的异步钩子被解析之后） 或终止 （导航到相同的路由、或在当前导航完成之前导航到另一个不同的路由） 的时候进行相应的调用。</p>
<ol start="2">
<li><strong><code>router.replace(location, onComplete?, onAbort?)</code></strong></li>
</ol>
<p>跟<code>router.push</code>很像，唯一的不同就是，它不会向<code>history</code>添加新记录，而是跟它的方法名一样，替换掉当前的 history 记录。</p>
<h2>location 的写法</h2>
<h3>不带参数</h3>
<p>例如：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">······</span><br><span class="line">&#123;</span><br><span class="line">    path:<span class="string">'/user'</span>,</span><br><span class="line">    name: BaseUser,</span><br><span class="line">    component: UserBase</span><br><span class="line">&#125;</span><br><span class="line">······</span><br></pre></td></tr></table></figure>
<ol>
<li>路径字符串：<code>'/user'</code>。</li>
<li>对象：<code>{path:'/user'}</code>。</li>
<li>命名路由：<code>{name:'BaseUser'}</code>。</li>
</ol>
<h3>带参数</h3>
<ol>
<li>对象：<code>{path:'/user',query:{userId:'123'}}</code>，通过<code>this.$route.query.userId</code>来获取。</li>
<li>命名路由：<code>{name:'BaseUser',params:{userId:'123'}}</code>，通过<code>this.$route.params.userId</code>来获取。</li>
</ol>
<p>以上两种对应的路由配置，基本没变：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">······</span><br><span class="line">&#123;</span><br><span class="line">    path:<span class="string">'/user'</span>,</span><br><span class="line">    name: BaseUser,</span><br><span class="line">    component: UserBase</span><br><span class="line">&#125;</span><br><span class="line">······</span><br></pre></td></tr></table></figure>
<p>**注意：如果给出的是<code>path</code>则对应的参数一定是<code>query</code>，而给出命名路由<code>name</code>一定是<code>params</code>。如果你想在<code>path</code>传参，但不想用<code>query</code>则需要手写完整的带有参数的<code>path</code>，如：<code>{path:/user/${userId}}</code>，但是此时需要在路由配置进行变化<code>path:'/user/:userId</code>，也是通过<code>this.$route.params.userId</code>来获取。</p>
<h1>命名视图</h1>
<p>有时候想同时 （同级） 展示多个视图，而不是嵌套展示，这个时候命名视图就派上用场了。你可以在界面中拥有多个单独命名的视图，而不是只有一个单独的出口。如果 <code>router-view</code>没有设置名字，那么默认为<code>default</code>。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">······</span><br><span class="line"><span class="tag">&lt;<span class="name">router-view</span> <span class="attr">class</span>=<span class="string">"view one"</span>&gt;</span><span class="tag">&lt;/<span class="name">router-view</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">router-view</span> <span class="attr">class</span>=<span class="string">"view two"</span> <span class="attr">name</span>=<span class="string">"a"</span>&gt;</span><span class="tag">&lt;/<span class="name">router-view</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">router-view</span> <span class="attr">class</span>=<span class="string">"view three"</span> <span class="attr">name</span>=<span class="string">"b"</span>&gt;</span><span class="tag">&lt;/<span class="name">router-view</span>&gt;</span></span><br><span class="line">······</span><br></pre></td></tr></table></figure>
<p>一个视图使用一个组件渲染，因此对于同个路由，多个视图就需要多个组件。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">······</span><br><span class="line">&#123;</span><br><span class="line">    path: <span class="string">'/'</span>,</span><br><span class="line">    components: &#123;<span class="comment">//多组件</span></span><br><span class="line">        <span class="keyword">default</span>: Foo,<span class="comment">//将Foo组件显示在默认视图上</span></span><br><span class="line">        a: Bar,<span class="comment">//将Bar组件显示在a视图上</span></span><br><span class="line">        b: Baz<span class="comment">//将Baz组件显示在b视图上</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">······</span><br></pre></td></tr></table></figure>
<p>注意，这里不是<code>component</code>，而是<code>components</code>，别打错了。</p>
<h1>重定向</h1>
<p>如果我们希望当访问某个页面时，跳转到其它页面时，这时候就可以运用重定向。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">······</span><br><span class="line">&#123;</span><br><span class="line">    path:<span class="string">'/a'</span>,</span><br><span class="line">    redirect: <span class="string">'/b'</span></span><br><span class="line">&#125;</span><br><span class="line">······</span><br></pre></td></tr></table></figure>
<p>当访问<code>/a</code>时就会跳转到<code>/b</code>。<code>redirect</code>写法与 [<code>local</code>参数写法】(#location 参数的写法）一样。甚至可以放一个方法：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    path:<span class="string">'/a'</span>,</span><br><span class="line">    redirect: <span class="function"><span class="params">to</span> =&gt;</span> &#123;</span><br><span class="line">      <span class="comment">// to 目标路由 作为参数</span></span><br><span class="line">      <span class="comment">// return 重定向的 字符串路径/路径对象</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">······</span><br></pre></td></tr></table></figure>
<h1>别名</h1>
<p>顾名思义，就是给某个路由起一个小名：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">······</span><br><span class="line">&#123;</span><br><span class="line">    path: <span class="string">'/a'</span>,</span><br><span class="line">    component: A,</span><br><span class="line">    alias: <span class="string">'/b'</span></span><br><span class="line">&#125;</span><br><span class="line">······</span><br></pre></td></tr></table></figure>
<p>上面当访问<code>/a</code>或<code>/b</code>时，都会跳转到 A 这个组件</p>
<h1>路由组件传参</h1>
<p>在组件中使用<code>$route</code>会使之与其对应路由形成高度耦合，从而使组件只能在某些特定的 URL 上使用，限制了其灵活性。</p>
<p>使用<code>props</code>将组件和路由解耦：</p>
<h2>$route</h2>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">······</span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span>User &#123;&#123; $route.params.id &#125;&#125;<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">······</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">······</span><br><span class="line">&#123;</span><br><span class="line">    path:<span class="string">'/user/:id'</span>,</span><br><span class="line">    component:User</span><br><span class="line">&#125;</span><br><span class="line">······</span><br></pre></td></tr></table></figure>
<p>具体请看<a href="#%E5%B8%A6%E5%8F%82%E6%95%B0">这里</a>，重点是<code>props</code>传参。</p>
<h2>通过 props 解耦</h2>
<p>vue 中：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> User = &#123;</span><br><span class="line">  props: [<span class="string">'id'</span>],</span><br><span class="line">  template: <span class="string">'&lt;div&gt;User &#123;&#123; id &#125;&#125;&lt;/div&gt;'</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>vue-cli 中：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">    &lt;div&gt;User &#123;&#123; id &#125;&#125;&lt;<span class="regexp">/div&gt;</span></span><br><span class="line"><span class="regexp">&lt;/</span>template&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  name: <span class="string">"success"</span>,</span><br><span class="line">  props：&#123;</span><br><span class="line">      id</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line">&lt;<span class="regexp">/script&gt;</span></span><br><span class="line"><span class="regexp">&lt;style scoped&gt;</span></span><br><span class="line"><span class="regexp">&lt;/</span>style&gt;</span><br></pre></td></tr></table></figure>
<p>在路由配置中，开启<code>props</code>选项：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">······</span><br><span class="line">&#123;</span><br><span class="line">    path: <span class="string">'/user/:id'</span>,</span><br><span class="line">    component: User,</span><br><span class="line">    props: <span class="literal">true</span></span><br><span class="line">&#125;</span><br><span class="line">······</span><br></pre></td></tr></table></figure>
<p>对于包含命名视图的路由，你必须分别为每个命名视图添加<code>props</code>选项：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">······</span><br><span class="line">&#123;</span><br><span class="line">    path: <span class="string">'/user/:id'</span>,</span><br><span class="line">    components: &#123;</span><br><span class="line">        <span class="keyword">default</span>: User,</span><br><span class="line">        sidebar: Sidebar</span><br><span class="line">    &#125;,</span><br><span class="line">    props: &#123;</span><br><span class="line">        <span class="keyword">default</span>: <span class="literal">true</span>,</span><br><span class="line">        sidebar: <span class="literal">false</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">······</span><br></pre></td></tr></table></figure>
<p>这样你便可以在任何地方使用该组件，使得该组件更易于重用和测试。</p>
<h1>响应路由参数的变化</h1>
<p>当从一个路由跳转到另一个路由时，如果组件相同，那么组件会被重新利用，而不是销毁重新创建，这也意味着组件的生命周期钩子不会再被调用。</p>
<p>复用组件时，想对路由参数的变化作出响应的话，你可以简单地 watch （监测变化） $route 对象：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">······</span><br><span class="line">watch: &#123;</span><br><span class="line">    <span class="string">'$route'</span> (to, <span class="keyword">from</span>) &#123;</span><br><span class="line">      <span class="comment">// 对路由变化作出响应...</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">······</span><br></pre></td></tr></table></figure>
<p>或者使用 2.2 中引入的 beforeRouteUpdate 导航守卫：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">······</span><br><span class="line">beforeRouteUpdate (to, <span class="keyword">from</span>, next) &#123;</span><br><span class="line">    <span class="comment">// react to route changes...</span></span><br><span class="line">    <span class="comment">// don't forget to call next()</span></span><br><span class="line">&#125;</span><br><span class="line">······</span><br></pre></td></tr></table></figure>

      
    </div>

    
      





    


    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>如果这篇文章对您很有帮助，不妨</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/blog/images/wechatpay.png" alt="付祥/lfagon 微信支付">
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/blog/images/alipay.jpg" alt="付祥/lfagon 支付宝">
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    
    
    	<div>
	   
             <div>
    
        <div style="margin-top: 30px;text-align:center;color: #ccc;font-size:14px;">-------------　　　　本文结束　<i class="fa fa-heart"></i>　感谢您的阅读　　　　-------------</div>
    
</div>
           
        </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/vue/" rel="tag"><i class="fa fa-tag"></i> vue</a>
          
            <a href="/blog/tags/vue-cli/" rel="tag"><i class="fa fa-tag"></i> vue-cli</a>
          
        </div>
      

      
      
        <div class="post-widgets">
        
          <div class="wp_rating">
            <div style="color: rgba(0, 0, 0, 0.75); font-size:13px; letter-spacing:3px">(&gt;看完记得五星好评哦亲&lt;)</div>
            <div id="wpac-rating"></div>
          </div>
        

        

        
          
          <div id="needsharebutton-postbottom">
            <span class="btn">
              <i class="fa fa-share-alt" aria-hidden="true"></i>
            </span>
          </div>
        
        </div>
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2019/06/11/激活码/" rel="next" title="激活码">
                <i class="fa fa-chevron-left"></i> 激活码
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2019/06/12/vuex/" rel="prev" title="vuex">
                vuex <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
        
  <div class="bdsharebuttonbox">
    <a href="#" class="bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a>
    <a href="#" class="bds_douban" data-cmd="douban" title="分享到豆瓣网"></a>
    <a href="#" class="bds_sqq" data-cmd="sqq" title="分享到QQ好友"></a>
    <a href="#" class="bds_qzone" data-cmd="qzone" title="分享到QQ空间"></a>
    <a href="#" class="bds_weixin" data-cmd="weixin" title="分享到微信"></a>
    <a href="#" class="bds_tieba" data-cmd="tieba" title="分享到百度贴吧"></a>
    <a href="#" class="bds_twi" data-cmd="twi" title="分享到Twitter"></a>
    <a href="#" class="bds_fbook" data-cmd="fbook" title="分享到Facebook"></a>
    <a href="#" class="bds_more" data-cmd="more"></a>
    <a class="bds_count" data-cmd="count"></a>
  </div>
  <script>
    window._bd_share_config = {
      "common": {
        "bdText": "",
        "bdMini": "2",
        "bdMiniList": false,
        "bdPic": ""
      },
      "share": {
        "bdSize": "24",
        "bdStyle": "0"
      },
      "image": {
        "viewList": ["tsina", "douban", "sqq", "qzone", "weixin", "twi", "fbook"],
        "viewText": "分享到：",
        "viewSize": "7"
      }
    }
  </script>

<script>
  with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='/static/api/js/share.js?cdnversion='+~(-new Date()/36e5)];
</script>

      
  </div>


          </div>
          

  
    <div class="comments" id="comments">
      
        <div id="gitment-container"></div>
      
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/blog/images/avatar.jpg" alt="付祥/lfagon">
            
              <p class="site-author-name" itemprop="name">付祥/lfagon</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">77</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/blog/categories/index.html">
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/blog/tags/index.html">
                  <span class="site-state-item-count">28</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/blog/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/lfagon" target="_blank" title="Github">
                      
                        <i class="fa fa-fw fa-github"></i></a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://gitee.com/lfagon" target="_blank" title="Gitee">
                      
                        <i class="fa fa-fw fa-git"></i></a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:slash7675@gmail.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i></a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://plus.google.com/" target="_blank" title="Google">
                      
                        <i class="fa fa-fw fa-google"></i></a>
                  </span>
                
            </div>
          

          
          
            <div class="cc-license motion-element" itemprop="license">
              <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" target="_blank">
                <img src="/blog/images/cc-by-nc-sa.svg" alt="Creative Commons">
              </a>
            </div>
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-globe"></i>
                圈子里住着一群可爱的程序猿&nbsp;<i class="fa fa-fw fa-globe fa-flip-horizontal"></i>
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="https://reuixiy.github.io/" title="一个Hexo博客玩到极致的大佬" target="_blank">一个Hexo博客玩到极致的大佬</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://mmmmasterc.github.io/" title="又菜又爱秀的哥们" target="_blank">又菜又爱秀的哥们</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.codesheep.cn/" title="乐于分享的华科Java程序羊(B站up主)" target="_blank">乐于分享的华科Java程序羊(B站up主)</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://me.idealli.com/" title="前端大神自称兰州小红鸡" target="_blank">前端大神自称兰州小红鸡</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#undefined"><span class="nav-number">1.</span> <span class="nav-text">介绍</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#undefined"><span class="nav-number">2.</span> <span class="nav-text">安装与配置</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#undefined"><span class="nav-number">3.</span> <span class="nav-text">优化路由配置</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#undefined"><span class="nav-number">4.</span> <span class="nav-text">匹配优先级</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#undefined"><span class="nav-number">5.</span> <span class="nav-text">捕获所有路由或 404 路由</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#undefined"><span class="nav-number">6.</span> <span class="nav-text">路由跳转</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#undefined"><span class="nav-number">6.1.</span> <span class="nav-text">声明式跳转</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#undefined"><span class="nav-number">6.2.</span> <span class="nav-text">编程式跳转</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#undefined"><span class="nav-number">6.3.</span> <span class="nav-text">location 的写法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#undefined"><span class="nav-number">6.3.1.</span> <span class="nav-text">不带参数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#undefined"><span class="nav-number">6.3.2.</span> <span class="nav-text">带参数</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#undefined"><span class="nav-number">7.</span> <span class="nav-text">命名视图</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#undefined"><span class="nav-number">8.</span> <span class="nav-text">重定向</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#undefined"><span class="nav-number">9.</span> <span class="nav-text">别名</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#undefined"><span class="nav-number">10.</span> <span class="nav-text">路由组件传参</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#undefined"><span class="nav-number">10.1.</span> <span class="nav-text">$route</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#undefined"><span class="nav-number">10.2.</span> <span class="nav-text">通过 props 解耦</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#undefined"><span class="nav-number">11.</span> <span class="nav-text">响应路由参数的变化</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
    </div>
  </aside>


        
      </div>
    </div></main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <font color="red"><i class="fa fa-heartbeat" aria-hidden="true"></i></font>
  </span>
  <span class="author" itemprop="copyrightHolder">copyright</span>
  &nbsp;<a href="https://www.oschina.net/"><font size="2px">中国开源社区</font></a>

  
</div>




<div style="width:300px;margin:0 auto; padding:0 0;">
   <a href style="display:inline-block;text-decoration:none;height:20px;line-height:20px;"><img src="/blog/images/icons/record.png" style="float:left;height:90%;width:auto;"><p style="float:left;height:20px;line-height:20px;margin: 0px 0px 0px 5px; color:#939393;letter-spacing:0px"><font size="2px">公网安备 xxxxxxxxxxx号</font></p></a>
</div>





        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user">&nbsp;访客量：</i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye">&nbsp;访问量：</i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      
    </span>
  
</div>




        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
          <span id="scrollpercent"><span>0</span>%</span>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>




















  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  


  

  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/image.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/linkcard.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/clicksocialvalue.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/clicklove.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  



	





  




  





<!-- LOCAL: You can save these files to your site and update links -->
    
        
        <link rel="stylesheet" href="https://aimingoo.github.io/gitmint/style/default.css">
        <script src="https://aimingoo.github.io/gitmint/dist/gitmint.browser.js"></script>
    
<!-- END LOCAL -->

    

    
      <script type="text/javascript">
      function renderGitment(){
        var gitment = new Gitmint({
            id: window.location.pathname, 
            owner: 'lfagon',
            repo: 'BlogComment',
            
            lang: "" || navigator.language || navigator.systemLanguage || navigator.userLanguage,
            
            oauth: {
            
            
                client_secret: 'de7d4aee11c1d7e4fd0ab75222817a4875fb378c',
            
                client_id: '969cf8ab6443ea4bd042'
            }});
        gitment.render('gitment-container');
      }

      
      renderGitment();
      
      </script>
    





  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/blog/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  
  
  
  <link rel="stylesheet" href="/blog/lib/needsharebutton/needsharebutton.css">

  
  
  <script src="/blog/lib/needsharebutton/needsharebutton.js"></script>

  <script>
    
      pbOptions = {};
      
          pbOptions.iconStyle = "box";
      
          pbOptions.boxForm = "horizontal";
      
          pbOptions.position = "bottomCenter";
      
          pbOptions.networks = "Weibo,Wechat,Douban,QQZone,Twitter,Facebook";
      
      new needShareButton('#needsharebutton-postbottom', pbOptions);
    
    
  </script>

  
  <script type="text/javascript">
  wpac_init = window.wpac_init || [];
  wpac_init.push({widget: 'Rating', id: 18607,
    el: 'wpac-rating',
    color: 'f79533'
  });
  (function() {
    if ('WIDGETPACK_LOADED' in window) return;
    WIDGETPACK_LOADED = true;
    var mc = document.createElement('script');
    mc.type = 'text/javascript';
    mc.async = true;
    mc.src = '//embed.widgetpack.com/widget.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(mc, s.nextSibling);
  })();
  </script>


  

  
<script>
  $('.highlight').each(function(i, e) {
    var $wrap = $('<div>').addClass('highlight-wrap');
    $(e).after($wrap);
    $wrap.append($('<button>').addClass('copy-btn').append('复制').on('click', function(e) {
      var code = $(this).parent().find('.code').find('.line').map(function(i, e) {
        return $(e).text();
      }).toArray().join('\n');
      var ta = document.createElement('textarea');
      var yPosition = window.pageYOffset || document.documentElement.scrollTop;
      ta.style.top = yPosition + 'px'; // Prevent page scroll
      ta.style.position = 'absolute';
      ta.style.opacity = '0';
      ta.readOnly = true;
      ta.value = code;
      document.body.appendChild(ta);
      ta.select();
      ta.setSelectionRange(0, code.length);
      ta.readOnly = false;
      var result = document.execCommand('copy');
      
        if (result) $(this).text('复制成功');
        else $(this).text('复制失败');
      
      ta.blur(); // For iOS
      $(this).blur();
    })).on('mouseleave', function(e) {
      var $b = $(this).find('.copy-btn');
      setTimeout(function() {
        $b.text('复制');
      }, 300);
    }).append(e);
  })
</script>


  

  

</body>
</html>
